﻿using System.Collections.Generic;
using System;
using BCService.Utils;
using BCCommon;
using BCData.TMS.RedPacket.RedPacketSupplement;
using BCDto.TMS.RedPacket.RedPacketSupplement;
using BCEntity.TMS.RedPacket.RedPacketSupplement;
using BCDto.Sim.Admin.Admin;
using BCData.TMS.RedPacket.RedPacket;
using BCEntity.Common.EntityNameConst;

namespace BCService.TMS.RedPacket.RedPacketSupplement
{
    public class RedPacketSupplementService : IRedPacketSupplementService
    {
        #region var
        private readonly IDatabaseContext databaseContext;
        private readonly IRedPacketSupplementData redPacketSupplementData;
        private readonly IRedisService redisService;
        private readonly IRedPacketData redPacketData;
        private readonly string key = string.Format("{0}", EntityNameConst.RedPacketSupplementEntity);
        #endregion

        #region 构造函数
        public RedPacketSupplementService(IRedPacketSupplementData redPacketSupplementData,
            IDatabaseContext databaseContext,
            IRedisService redisService,
            IRedPacketData redPacketData)
        {
            this.redPacketSupplementData = redPacketSupplementData;
            this.databaseContext = databaseContext;
            this.redisService = redisService;
            this.redPacketData = redPacketData;
        }
        #endregion

        #region Delete
        /// <summary>
        /// 删除RedPacketSupplement记录
        /// </summary>
        /// <param name="redPacketSupplementId"></param>
        /// <returns></returns>
        public void Delete(long redPacketSupplementId)
        {
            redPacketSupplementData.DeleteAsync(redPacketSupplementId).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(key, false);
        }
        #endregion

        #region GetByRedPacketSupplementId
        /// <summary>
        /// 获取RedPacketSupplement记录
        /// </summary>
        /// <param name="redPacketSupplementId"></param>
        /// <returns></returns>
        public RedPacketSupplementDto GetById(long redPacketSupplementId)
        {
            var a = redPacketSupplementData.GetEntityAsync(redPacketSupplementId).GetAwaiter().GetResult();
            return a.As<RedPacketSupplementDto>();
        }
        #endregion

        #region GetList  获取RedPacketSupplement记录--[分页]
        public PageableList<RedPacketSupplementDto> GetList(AdminDto admin,long? redPacketId, int pageIndex, int pageSize)
        {
            if (admin.AgentRecord==null)
            {
                throw new ArgumentException(" 用户认证失败,错误操作 ");
            }
            if (redPacketId.HasValue)
            {
                var redPacketEntity = this.redPacketData.GetSingle(redPacketId.Value).Result;
                if (redPacketEntity == null)
                {
                    throw new ArgumentException(" 当前钱包不存在,不可查看 ");
                }
                if (redPacketEntity.AgentRecordId != admin.AgentRecord.AgentRecordId)
                {
                    throw new ArgumentException(" 不可查看不属于自己代理商的红包 ");
                }
            }
            string cacheKey = this.redisService.GetKey(key, admin.AgentRecord.AgentRecordId, redPacketId,
                    pageIndex, pageSize);
            var resultRedis = this.redisService.TryGet(cacheKey, () =>
            {
                var (fList, fCount) = redPacketSupplementData.GetListAsync(admin.AgentRecord.AgentRecordId, redPacketId, 
                    pageIndex, pageSize).GetAwaiter().GetResult();
                return new PageableList<RedPacketSupplementDto>
                {
                    Count = fCount,
                    Items = fList.As<IEnumerable<RedPacketSupplementDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now,
                };
            }, TimeSpan.FromDays(1));
            return resultRedis;
        }
        #endregion
    }
}